package net.chasing.androidbaseconfig.util;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.StringRes;

import net.chasing.androidbaseconfig.R;
import net.chasing.androidbaseconfig.util.thread.ThreadPoolUtils;

/**
 * 全局Toast使用
 * Android11在系统桌面禁止谈出自定义Toast，所以需要进行一个判断
 * Toast.LENGTH_SHORT是2000ms
 * Toast.LENGTH_LONG是3500ms
 */
public class ToastUtils {
    private static volatile Toast mToast;
    private static final Object CLOCK = new Object();

    public static void showShort(Context context, String msg) {
        if (context == null) return;
        if (!TextUtils.isEmpty(msg)) {
            ThreadPoolUtils.getInstance().executeOnMainThread(() -> {
                if (ActivityUtils.StartActivityCount == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                } else {
                    initToast(context.getApplicationContext());
                    View view = mToast.getView();
                    if (view != null) {
                        ((TextView) view.findViewById(R.id.customToast)).setText(msg);
                    }
                    mToast.setDuration(Toast.LENGTH_SHORT);
                    mToast.show();
                }
            });
        }
    }

    public static void showShort(Context context, @StringRes int stringId) {
        if (context == null) return;
        ThreadPoolUtils.getInstance().executeOnMainThread(() -> {
            if (ActivityUtils.StartActivityCount == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                Toast.makeText(context, stringId, Toast.LENGTH_SHORT).show();
            } else {
                initToast(context.getApplicationContext());
                View view = mToast.getView();
                if (view != null) {
                    ((TextView) view.findViewById(R.id.customToast)).setText(stringId);
                }
                mToast.setDuration(Toast.LENGTH_SHORT);
                mToast.show();
            }
        });
    }

    public static void showLong(Context context, String msg) {
        if (context == null) return;
        if (!TextUtils.isEmpty(msg)) {
            ThreadPoolUtils.getInstance().executeOnMainThread(() -> {
                if (ActivityUtils.StartActivityCount == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
                } else {
                    initToast(context.getApplicationContext());
                    View view = mToast.getView();
                    if (view != null) {
                        ((TextView) view.findViewById(R.id.customToast)).setText(msg);
                    }
                    mToast.setDuration(Toast.LENGTH_LONG);
                    mToast.show();
                }
            });
        }
    }

    public static void showLong(Context context, @StringRes int stringId) {
        if (context == null) return;
        ThreadPoolUtils.getInstance().executeOnMainThread(() -> {
            if (ActivityUtils.StartActivityCount == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                Toast.makeText(context, stringId, Toast.LENGTH_LONG).show();
            } else {
                initToast(context.getApplicationContext());
                View view = mToast.getView();
                if (view != null) {
                    ((TextView) view.findViewById(R.id.customToast)).setText(stringId);
                }
                mToast.setDuration(Toast.LENGTH_LONG);
                mToast.show();
            }
        });
    }

    /**
     * 每次使用新的View进行展示，避免出现系统时序上的一个bug：view had already added
     */
    private static void initToast(Context context) {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
        if (mToast == null) {
            synchronized (CLOCK) {
                if (mToast == null) {
                    mToast = new Toast(context);
                    mToast.setGravity(Gravity.CENTER, 0, 0);
                }
            }
        }
        View view = View.inflate(context, R.layout.custom_toast, null);
        mToast.setView(view);
    }
}
